/**
 * 
 */
package com.surelution.cppei.card

import org.apache.commons.io.IOUtils;
import org.apache.http.Consts
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.NameValuePair
import org.apache.http.client.HttpClient
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.message.BasicNameValuePair
import org.apache.http.util.EntityUtils

import com.surelution.cppei.weixin.gate.SubscriberGascard;

/**
 * @author <a href="mailto:guangzong.syu@gmail.com">guangzong</a>
 *
 */
class CompanyCardInfo extends CardInfo {
	
	private HttpEntity entity;
	
	private NodeList getNodeList(String api, String userid) {
		String url = "${BASE_API_URL}${api}";
		HttpPost post = new HttpPost(url);
		HttpClient client = new DefaultHttpClient();
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("key", "7/ZM+0PP9xTxMvQKPjpgYg=="));
		nvps.add(new BasicNameValuePair("UserId", userid));
		post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
		HttpResponse response = client.execute(post);
		entity = response.getEntity();
		InputStream is = entity.getContent();
		
		String line = ""
		IOUtils.readLines(is).each {
			line += it
			line += "\n"
		}
		
		println line
		
		def records = new XmlParser().parseText(line).CONSSET.singleItem
		return records
	}
	
	private void consume() {
		EntityUtils.consume(entity)
	}
	/* (non-Javadoc)
	 * @see com.surelution.cppei.card.CardInfo#query(java.lang.String)
	 */
	@Override
	public CardInfo query(String cardNo) {
		this.cardNo = cardNo
		def card = SubscriberGascard.findByCardNoAndCertificated(cardNo, true)
		def userid = card.userid
		def nodes = getNodeList("getCardUserInfo.action", userid);
		balance = nodes.find() {item->
			item.'@name'.contains('balance')
		}.text()
		println balance
		
		loyaltyBalance = nodes.find() {item->
			item.'@name'.contains('loyaltyBalance')
		}.text()
		
//		cardNo = nodes.find() {item->
//			item.'@name'.contains('asn')
//		}.text()
		
		consume();
		
		
		
		
		String url = "${BASE_API_URL}getDriverInfo.action";
		HttpPost post = new HttpPost(url);
		HttpClient client = new DefaultHttpClient();
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("key", "7/ZM+0PP9xTxMvQKPjpgYg=="));
		println cardNo
		nvps.add(new BasicNameValuePair("cardAsn", cardNo));
		
		nvps.add(new BasicNameValuePair("orgUserId", "0"));
		nvps.add(new BasicNameValuePair("start", "0"));
		nvps.add(new BasicNameValuePair("maxResult", "0"));
		post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
		HttpResponse response = client.execute(post);
		entity = response.getEntity();
		InputStream is = entity.getContent();
		
		String line = ""
		IOUtils.readLines(is).each {
			line += it
			line += "\n"
		}
		
		println line
		
		def records = new XmlParser().parseText(line).CONSSET.multiItem.singleItem
		
		cardBalance = records.find() {item->
			item.'@name'.contains('cardBalance')
		}.text()
		cardLoyaltyBalance = records.find() {item->
			item.'@name'.contains('cardLoyaltyBalance')
		}.text()
		println balance
		
		return this;
	}

	private String balance
	private String loyaltyBalance
	private String cardNo //asn
	private String cardBalance
	private String cardLoyaltyBalance
	
	public String getCardBalance() {
		cardBalance
	}
	
	public String getCardLoyaltyBalance() {
		cardLoyaltyBalance
	}

	public String getBalance() {
		balance
	}

	public String getLoyaltyBalance() {
		loyaltyBalance
	}
	
	public String getCardNo() {
		cardNo
	}

}
